package com.fowo.api.syncAmazon.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fowo.api.datainterface.service.DatainterfaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @fileName: AmazonAdvertisAuthAction
 * @Description: 代码目的，作用，如何工作
 * @Author: yyf
 * @Date: 2024/3/5 0005 14:02
 */
@Component
@Slf4j
public class AmazonAdvertAuthAction {
    @Resource
    private DatainterfaceService datainterfaceService;

    public static final String GRANT_TYPE = "refresh_token";
    public static final String CLIENT_ID = "amzn1.application-oa2-client.e6756e603c98473c9634c3d01a9053a7";
    public static final String CLILENT_SECRET = "amzn1.oa2-cs.v1.cc07047873cf7f6685ff5ef6f6650cdb3e72d43c101a60a49f7d043d87ce0e12";

    /**
     * 从亚马逊获取账号店铺信息
     * @param token 账号token
     * @return
     */
    public JSONArray getProfiles(String token){
        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization","Bearer "+token);
        headers.put("Amazon-Advertising-API-ClientId",CLIENT_ID);
        Object res = datainterfaceService.executeInterface(null, "EC00565", null, null, headers);
        JSONArray sellerShopInfo = JSONArray.parseArray(res.toString());
        return sellerShopInfo;
    }

    /**
     * 请求token
     * @param refreshToken
     * @return
     */
    public JSONObject getTokenInfo(String refreshToken) {
        Map<String,String> paramMap = new HashMap<>(4);
        paramMap.put("grant_type",GRANT_TYPE);
        paramMap.put("refresh_token",refreshToken);
        paramMap.put("client_id",CLIENT_ID);
        paramMap.put("client_secret",CLILENT_SECRET);
        Object res = datainterfaceService.executeInterface(null, "EC00561", null, paramMap, null);
        JSONObject tokenMap = JSONObject.parseObject(res.toString());
        return tokenMap;
    }
    public String getToken(String refreshToken){
        JSONObject tokenInfo = getTokenInfo(refreshToken);
        return tokenInfo.getString("access_token");
    }

    public Object getInvoices(String token,String profileId,Object query){
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization","Bearer " + token);
        headers.put("Amazon-Advertising-API-ClientId",CLIENT_ID);
        headers.put("Amazon-Advertising-API-Scope",profileId);
        Object res = datainterfaceService.executeInterface(null, "EC00568", null, query, headers);
        return res;
    }

    public Object getInvoiceByInvoiceId(String token,String profileId,Object query){
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization","Bearer " + token);
        headers.put("Amazon-Advertising-API-ClientId",CLIENT_ID);
        headers.put("Amazon-Advertising-API-Scope",profileId);
        Object res = datainterfaceService.executeInterface(null, "EC00571", null, query, headers);
        return res;
    }
}
